<template>
  <div class="cont">
    <div class="chart"></div>
  </div>
</template>

<script>
import echarts from 'echarts';
require('echarts/theme/macarons'); // echarts theme
import { debounce } from '@/utils';

export default {
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener('resize', this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener('resize', this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');

      this.chart.setOption({
        title: {
          text: '套餐数据',
          show: true,
          textStyle: {
            color: '#666',
            fontSize: 18,
            fontWeight: 'bold'
          }
        },
        toolbox: {
          show: true,
          feature: {
            saveAsImage: {}
          }
        },
        // tooltip: {
        //   trigger: 'item',
        //   formatter: '{a} <br/>{b} : {c} ({d}%)'
        // },
        legend: {
          bottom: 0,
          itemGap: 32,
          itemWidth: 16,
          itemHeight: 16,
          textStyle: { color: '#666', fontSize: 11 },
          data: ['罗慕Fit-4套餐', '罗慕Fit-2套餐', 'All-in-1套餐']
        },
        series: [
          {
            name: '套餐数据',
            type: 'pie',
            radius: '55%',
            center: ['50%', '45%'],
            data: [
              { value: 120, name: '罗慕Fit-4套餐', itemStyle: { color: '#42A2FF' } },
              { value: 100, name: '罗慕Fit-2套餐', itemStyle: { color: '#2EC9C7' } },
              { value: 80, name: 'All-in-1套餐', itemStyle: { color: '#F6AB6E' } }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          },
          {
            name: '外边框',
            type: 'pie',
            clockWise: false,
            radius: ['60%', '60%'],
            center: ['50%', '45%'],
            data: [{
                value: 10,
                itemStyle: {
                  normal: {
                    borderWidth: 1,//设置边框粗细
                    borderColor: '#D8E4F6'//边框颜色
                  }
              }
            }]
          }
        ]
      });
    }
  }
};
</script>

<style scoped>
    .cont {
        width: calc(100% - 40px);
        height: 350px;
        padding: 20px;
        border: 1px solid #E4E4E4;
        border-radius: 10px;
        box-sizing: content-box;
    }
</style>
